import plugin from '../../../lib/plugins/plugin.js';  
import fetch from 'node-fetch';  
  
export class ShortUrl extends plugin {  
  constructor() {  
    super({  
      name: '获取网站标题',  
      dsc: '网站标题查询服务',  
      event: 'message',  
      priority: 5000,  
      rule: [  
        {  
          reg: `^#(.*)(网站标题查询)(.*)`,  
          fnc: 'shortUrlQuery'  
        },  
      ]  
    });  
  }  
  async shortUrlQuery(e) {  
    let msg = e.msg;  
    let place = msg.replace(/#|网站标题查询/g, "").trim();  
    let url = `https://v2.api-m.com/api/title?url=${place}`; // 修改后的URL，包含查询参数  
    let response = await fetch(url);  
    let data = await response.json(); // 将响应解析为JSON对象  
    let title = data.data; // 提取data字段中的数据  
  
    if (!title) {  
      logger.error('[网站标题查询] 接口请求失败或未返回标题');  
      return await this.reply('网站标题查询接口请求失败,请联系主人更换接口');  
    }  
    await this.reply(`标题: ${title}`); // 回复标题给发送者  
  }  
}
